#########################
#   Author : Leon yu    #
#   Date : 2025/06/06   #
#   Id : SM2772         #
#########################
import json
from jsonpath_ng import jsonpath, parse
from dbResps.businessOverview import *
from apiRespsAnalyze.businessOverviewResultFromApi import *

class Compare(object):
    
    @staticmethod
    def compare(data1:dict, data2:dict):
        if data1 and data2:
            try:
                if len(data1) != len(data2):
                    Log.error("two compare Object is not same")
                    return 
                errorMsgs = []
                for key in data1.keys():
                    jsonpath_expr = parse(f'$.{key}')
                    value1 = jsonpath_expr.find(data1)[0].value
                    value2 = jsonpath_expr.find(data2)[0].value
                    if value1 != value2:
                        errorMsgs.append(f"Key '{key}' mismatch: {value1} != {value2},  ")
                if len(errorMsgs) > 0:
                    Log.error(f"Compare is Fail {errorMsgs}")
                    return False
                return True
            except Exception as e:
                Log.error(f"error : {e}")
                return 
        else:
            Log.error(f"The two objects of comparison are Incorrect")
            return 
        
    @staticmethod
    def compare_json(json1, json2):
        if isinstance(json1, dict) and isinstance(json2, dict):
            if json1.keys() != json2.keys():
                return False
            for key in json1.keys():
                if not Compare.compare_json(json1[key], json2[key]):
                    return False
            return True
        elif isinstance(json1, list) and isinstance(json2, list):
            if len(json1) != len(json2):
                return False
            for i in range(len(json1)):
                if not Compare.compare_json(json1[i], json2[i]):
                    return False
            return True
        else:
            return json1 == json2
